{% extends "projects/base_project.html" %}

{% load core_tags i18n static %}

{% block title %}
  {% blocktrans with query=query|default:"" %}
    Search: {{ query }}
  {% endblocktrans %}
{% endblock %}

{% block extra_links %}
  {{ super }}
  <style>
    .navigable > ul > li { border-bottom: 0; }
  </style>
{% endblock %}

{% block project_editing %}
  {% if project_obj %}
    {% with search_active="active" project=project_obj %}
        {% include "core/project_bar.html" %}
    {% endwith %}
  {% endif %}
{% endblock %}

{% block content %}

<div class="navigable">
  <ul>
    <h5>{% trans 'Object Type' %}</h5>
    {% if not project_obj %}
      <li class="{% if type == 'project' %}active{% endif %}"><a href="?{% url_replace request 'type' 'project' %}">{% trans 'Projects' %}</a></li>
    {% endif %}
      <li class="{% if type == 'file' %}active{% endif %}"><a href="?{% url_replace request 'type' 'file' %}">{% trans 'Files' %}</a></li>
    <hr>

    {% if facets.project and not project_obj %}
      <h5>{% trans 'Projects' %}</h5>

      <li class="project-list">
        <a href="?{% url_replace request 'q' search_query %}">
          {% trans 'Search all' %}
        </a>
      </li>

      {% for name, count, selected in facets.project %}
        <li class="{% if selected %}active{% endif %} project-list">
          <a href="?{% url_replace request 'q' 'project:' name ' ' search_query %}">
            {{ name }} ({{ count }})
          </a>
        </li>
      {% endfor %}
      <hr>
    {% endif %}

    {% if facets.language %}
      <h5>{% trans 'Language' %}</h5>
      {% for name, count, selected in facets.language %}
        <li class="{% if selected %}active{% endif %} language-list">
          {% if language == name %}
            <a href="?{% url_replace request 'language' ''  %}">{{ name }}
          {% else %}
            <a href="?{% url_replace request 'language' name  %}">{{ name }}
          {% endif %}
          ({{ count }})
          </a>
        </li>
      {% endfor %}
      <hr>
    {% endif %}

    {% block sponsor %}
      <div class="search-sponsored">
        Search is sponsored by <a href="https://www.elastic.co/">Elastic</a>, and hosted on <a href="https://www.elastic.co/cloud">Elastic Cloud</a>.
      </div>
    {% endblock sponsor %}
  </ul>
<div>

<h2>{% trans 'Search' %}</h2>
<!-- BEGIN search form -->
<div class="wide-search-bar">
  <div class="wide-search-bar-wrapper clearfix">
    <form action="." method="GET">
      <div class="text-input-wrapper">
        <input type="text" name="q" value="{{ query|default_if_none:"" }}" id="id_elastic_search">
        {% if type %} <input type="hidden" name="type" value="{{ type }}"> {% endif %}
        {% if language %} <input type="hidden" name="language" value="{{ language }}"> {% endif %}
      </div>
      <div class="submit-input-wrapper">
        {% comment %}Translators: This is about starting a search{% endcomment %}
        <input type="submit" value="{% trans 'Search' %}">
      </div>
    </form>
  </div>
</div>
<!-- END search form -->

{% if query %}
  <!-- BEGIN search results -->
  <div class="module">
    <div class="module-wrapper">
      <div class="module-header">
        <h3>
          {% blocktrans with count=total_count|default:"0" query=search_query|default:"" %}
            {{ count }} results for <code>{{ query }}</code>
          {% endblocktrans %}
        </h3>
      </div>

      <div class="module-list">
        <div class="module-list-wrapper">
          <ul>

            {% with "100" as MAX_SUBSTRING_LIMIT %}
            {% for result in results %}

              <li class="module-item search-result-item">
                <p class="module-item-title">
                  {% if result.type == 'project' %}
                    <a href="{{ result.link }}">
                      {{ result.name }} (<em>{{ result.slug }}</em>)
                    </a>
                    {% for fragment in result.highlights.description %}
                      <p class="fragment">
                        ...{{ fragment|safe }}...
                      </p>
                    {% empty %}
                      {% if result.description %}
                        <p>{{ result.description|slice:MAX_SUBSTRING_LIMIT }}</p>
                      {% else %}
                        <p><em>No description for project.</em></p>
                      {% endif %}
                    {% endfor %}

                  {% elif result.type == 'page' %}
                      <a href="{{ result.domain }}{{ result.path}}?highlight={{ search_query|urlencode }}">
                        {{ result.project.slug }} - {% if result.highlights.title %} {{ result.highlights.title.0|safe }} {% else %} {{ result.title }} {% endif %}
                      </a>

                      {% for block in result.blocks %}
                        {% if block.type == 'domain' %}
                          <p>
                            <a href="{{ result.domain }}{{ result.path }}?highlight={{ search_query|urlencode }}#{{ block.id }}">
                              {% if block.highlights.name %}
                                {% with domain_name=block.highlights.name %}
                                  [{{ block.role }}]: {{ domain_name.0|safe }}
                                {% endwith %}
                              {% else %}
                                [{{ block.role }}]: {{ block.name }}
                              {% endif %}
                            </a>
                          </p>
                          <p class="fragment">
                            {% if block.highlights.content %}
                              {% with domain_content=block.highlights.content %}
                                {{ domain_content.0|safe }}
                              {% endwith %}
                            {% else %}
                              {% if block.content %}
                                {{ block.content|slice:MAX_SUBSTRING_LIMIT }} ...
                              {% endif %}
                            {% endif %}
                          </p>

                        {% elif block.type == 'section' %}
                          <p>
                            <a href="{{ result.domain }}{{ result.path }}?highlight={{ search_query|urlencode }}#{{ block.id }}">
                              {% if block.highlights.title %}
                                {% with section_title=block.highlights.title %}
                                  {{ section_title.0|safe }}
                                {% endwith %}
                              {% else %}
                                {{ block.title }}
                              {% endif %}
                            </a>
                          </p>
                          {% if block.highlights.content %}
                            {% with section_content=block.highlights.content %}
                              {% for content in section_content %}
                                <p class="fragment">
                                  ... {{ content|safe }} ...
                                </p>
                              {% endfor %}
                            {% endwith %}
                          {% else %}
                            <p class="fragment">
                              {{ block.content|slice:MAX_SUBSTRING_LIMIT }} ...
                            </p>
                          {% endif %}
                        {% endif %}
                      {% endfor %}
                  {% endif %}
                </p>
              </li>
            {% empty %}
              <li class="module-item"><span class="quiet">{% trans "No results found." %}</span></li>
            {% endfor %}
            {% endwith %}
          </ul>
        </div>
      </div>
    </div>
  </div>
{% endif %}
<!-- END search results -->
</div>
</div>
{% endblock %}
